package com.jcroke.PC32.memory;

import java.util.LinkedHashMap;

import com.jcroke.PC32.datum.Datum;

/**
 * 
 * @author jcroke
 * 
 */
public class Umem {

	private final LinkedHashMap<String, PnodeMemoryFrame> _uFlsh;

	public Umem() {
		this._uFlsh = new LinkedHashMap<String, PnodeMemoryFrame>();
	}

	public final Datum read(final String p, final String address) {
		Datum rDatum = null;
		if (_uFlsh.containsKey(p)) {
			rDatum = (_uFlsh.get(p).getFe(address));
		}
		String rDatumDebug = (rDatum == null ? "Non-Existent" : rDatum.toString());
		System.out.println("<-[Read Mem]" + " NODE: " + p + " ADDRESS: "
				+ address + " VALUE = '" + rDatumDebug + "'");
		return rDatum;
	}

	public synchronized final void write(final String n, final String address,
			final Datum value) {
		if (!_uFlsh.containsKey(n)) {
			_uFlsh.put(n, new PnodeMemoryFrame());
		}
		_uFlsh.get(n).setFe(address, value);
		System.out.println("->[Write Mem] " + "TYPE: ("
				+ value.getType().getName() + ") ADDRESS: " + address
				+ " VALUE: '" + value + "'");
	}

	public void clear() {
		this._uFlsh.clear();
		System.out.println("[[MEMORY CLEARED]]");
	}

	public synchronized void write(final String n,
			final LinkedHashMap<String, Datum> src) {
		this._uFlsh.get(n).setF(src);
	}

	public LinkedHashMap<String, Datum> read(final String n) {
		return ((_uFlsh.get(n).getF()));
	}
}
